package com.pan.basic;

public class ReturnTest {

    public static Integer testFinally(Integer val){

        try{

            if(val%2==0){
                val++;
            }
            return val;
        }finally{
            val++;
        }

    }

    public static Integer testCatch(Integer val){

        try{

            if(val%2==0){
                val = val/0;
            }
            return val;
        }catch (Exception e){
            val++;
        }

        return val;
    }

    public static Integer testCatchFinally(Integer val){

        try{

            if(val%2==0){
                val = val/0;
            }
            return val;
        }catch (Exception e){
            val++;
            System.out.println("testCatchFinally-->log-->catch-->"+val);
        }finally{
            val++;
            System.out.println("testCatchFinally-->log-->finally-->"+val);
        }

        return val;
    }

    public static Integer testReturnCatchFinally(Integer val){

        try{

            if(val%2==0){
                val = val/0;
            }
            return val;
        }catch (Exception e){
            return val++;
        }finally{
            val++;
            System.out.println("testReturnCatchFinally-->log-->finally-->"+val);
        }

    }

    public static Integer testCatchReturnFinally(Integer val){

        try{

            if(val%2==0){
                val = val/0;
            }
            return val;

        }catch (Exception e){
             val++;
        }finally{
            return val++;
        }
    }

    /**
     * val++修改的是局部变量表，只有在再次加载的时候，才会用到当前局部变量表的值。
     * return的时候，直接取得栈顶的值。
     * @param args
     */
    public static void main(String[] args) {
        //
        Integer integer1 = testFinally(10);
        System.out.println("testFinally-->"+integer1);

        Integer integer2 = testCatch(10);
        System.out.println("testCatch-->"+integer2);

        Integer integer3 = testCatch(9);
        System.out.println("testCatch-->"+integer3);

        Integer integer4 = testCatchFinally(9);
        System.out.println("testCatchFinally-->"+integer4);

        Integer integer5 = testCatchFinally(10);
        System.out.println("testCatchFinally-->"+integer5);

        Integer integer6 = testReturnCatchFinally(10);
        System.out.println("testReturnCatchFinally-->"+integer6);

        Integer integer7 = testCatchReturnFinally(10);
        System.out.println("testCatchReturnFinally-->"+integer7);
    }
}
